package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.feign.BusinessFeignClient;
import com.neusoft.feign.FoodFeignClient;
import com.neusoft.po.Business;
import com.neusoft.po.Cart;
import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.CartService;
@RefreshScope
@RestController
@RequestMapping("/CartController")
public class CartController {
	@Autowired
	private CartService cartService;
	@Autowired
	private FoodFeignClient foodFeignClient;
	@Autowired
	private BusinessFeignClient businessFeignClient;

	@GetMapping("/listCart/{userId}")
	public CommonResult<List> listCart(@PathVariable("userId") String userId) throws Exception {
		Cart param = new Cart();
		param.setUserId(userId);
		List<Cart> list = cartService.listCart(param);
		for(Cart cart:list) {
			CommonResult<Food> FoodResult = foodFeignClient.getFoodByFoodId(cart.getFoodId());
			if(FoodResult.getCode() == 200) {
				Food food = FoodResult.getResult();
				cart.setFood(food);
				}
			else {
				cart.setFood(null);
			}
			CommonResult<Business> BusinessResult = businessFeignClient.getBusinessById(cart.getBusinessId());
			if(BusinessResult.getCode()==200) {
				Business business = BusinessResult.getResult();
				cart.setBusiness(business);
			}
			else {
				cart.setBusiness(null);
			}
		}
		return new CommonResult(200, "success(10400)", list);
	}

	@GetMapping("/listCart/{userId}/{businesssId}")
	public CommonResult<List> listCart(@PathVariable("userId") String userId,
			@PathVariable("businesssId") Integer businesssId) throws Exception {
		Cart param = new Cart();
		param.setUserId(userId);
		param.setBusinessId(businesssId);
		List<Cart> list = cartService.listCart(param);
		for(Cart cart:list) {
			CommonResult<Food> FoodResult = foodFeignClient.getFoodByFoodId(cart.getFoodId());
			if(FoodResult.getCode() == 200) {
				Food food = FoodResult.getResult();
				cart.setFood(food);
				}
			else {
				cart.setFood(null);
			}
			CommonResult<Business> BusinessResult = businessFeignClient.getBusinessById(cart.getBusinessId());
			if(BusinessResult.getCode()==200) {
				Business business = BusinessResult.getResult();
				cart.setBusiness(business);
			}
			else {
				cart.setBusiness(null);
			}
		}
		return new CommonResult(200, "success(10400)", list);
	}

	@DeleteMapping("/removeCart/{userId}/{businessId}/{foodId}")
	public CommonResult<Integer> removeCart(@PathVariable("userId") String userId,
			@PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) throws Exception {
		Cart param = new Cart();
		param.setUserId(userId);
		param.setBusinessId(businessId);
		param.setFoodId(foodId);
		int result = cartService.removeCart(param);
		return new CommonResult(200, "success", result);
	}

	@PostMapping("/saveCart/{userId}/{businessId}/{foodId}")
	public CommonResult<Integer> saveCart(@PathVariable("userId") String userId,
			@PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId) throws Exception {
		Cart param = new Cart();
		param.setUserId(userId);
		param.setBusinessId(businessId);
		param.setFoodId(foodId);
		int result = cartService.saveCart(param);
		return new CommonResult(200, "success", result);
	}

	@PutMapping("/updateCart/{userId}/{businessId}/{foodId}/{quantity}")
	public CommonResult<Integer> updateCart(@PathVariable("userId") String userId,
			@PathVariable("businessId") Integer businessId, @PathVariable("foodId") Integer foodId,
			@PathVariable("quantity") Integer quantity) throws Exception {
		Cart param = new Cart();
		param.setUserId(userId);
		param.setBusinessId(businessId);
		param.setFoodId(foodId);
		param.setQuantity(quantity);
		int result = cartService.updateCart(param);
		return new CommonResult(200, "success", result);
	}

}
